package oj;

//返回链表的倒数第k个节点
public class Num22 {
    public ListNode getKthFromEnd(ListNode head, int k) {
        if(head == null || k == 0){
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;

        for (int i = 0; i < k - 1; i++) {
            if(fast.next != null){
                fast = fast.next;
            }else {
                return null;
            }
        }
        while (fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
